package com.xcenter.cabinet.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xcenter.cabinet.file.service.FileService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("cabinet/file")
public class FileController {

	@Autowired
	private FileService fileService;

	@ApiOperation(value = "上传文件")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "form", name = "bucket", value = "文件夹", dataType = "string", required = true),
		@ApiImplicitParam(paramType = "form", name = "file", value = "文件", dataType = "file", required = true) 
	})
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(String bucket, MultipartFile file) {
		return uploads(bucket, new MultipartFile[] { file });
	}

	@ApiOperation(value = "批量上传文件")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "form", name = "bucket", value = "文件夹", dataType = "string", required = true),
		@ApiImplicitParam(paramType = "form", name = "files", value = "文件1", dataType = "file", required = true),
		@ApiImplicitParam(paramType = "form", name = "files", value = "文件2", dataType = "file", required = true) 
	})
	@RequestMapping(value = "/uploads", method = RequestMethod.POST)
	public String uploads(String bucket, MultipartFile[] files) {
		if (StringUtils.isEmpty(bucket) || files == null || files.length == 0) {
			return "{\"succcess\": false, \"message\": \"参数错误\"}";
		}
		fileService.upload(bucket, files);
		return null;
	}

}
